package com.donggua.aichebao.commonui.maintain;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.donggua.aichebao.commonui.R;

/**
 * Created by Administrator on 2017-07-23.
 */

public class MaintainRecordItemLayout extends RelativeLayout {
    private TextView tvLeft;
    private TextView tvRight;
    private int textSizeType = TypedValue.COMPLEX_UNIT_PX;
    public MaintainRecordItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomMaintain);
        String name = ta.getString(R.styleable.CustomMaintain_maintainName);
        String content = ta.getString(R.styleable.CustomMaintain_maintainContent);
        LayoutParams leftParams = getMyLayoutParams();
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
        tvLeft = getTextView(context);
        tvLeft.setText(name);
        tvLeft.setLayoutParams(leftParams);

        LayoutParams rightParams = getMyLayoutParams();
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
        tvRight = getTextView(context);
        tvRight.setText(getContentText(content));
        tvRight.setLayoutParams(leftParams);
        this.addView(tvLeft);
        this.addView(tvRight);
        ta.recycle();
    }

    private TextView getTextView(Context context) {
        TextView textView = new TextView(context);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(textSizeType,18);
        return textView;
    }

    private String getContentText(String text){
        StringBuilder builder = new StringBuilder();
        builder.append("(");
        builder.append(text);
        builder.append(")");
        return builder.toString();
    }

    private static LayoutParams getMyLayoutParams(){
        LayoutParams layoutParams = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
        return layoutParams;
    }

    public void setNumber(int number){
        tvRight.setText(getContentText(String.valueOf(number)));
    }
}
